package com.xoms.front.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.front.cache.dao.PayCacheDao;
import com.xoms.front.controller.res.BaseResult;
import com.xoms.front.controller.res.ObjectResult;
import com.xoms.front.controller.res.bean.ROrderStatusBean;
import com.xoms.front.controller.springmvc.req.GetALIScanAuthCode2StoreParam;
import com.xoms.front.controller.springmvc.req.GetALIScanQrcode2StoreParam;
import com.xoms.front.controller.springmvc.req.GetQueryOrderStatusParam;
import com.xoms.front.controller.springmvc.req.GetWXScanAuthCode2StoreParam;
import com.xoms.front.controller.springmvc.req.GetWXScanQrcode2StoreParam;
import com.xoms.front.service.PayService;
import com.xoms.front.service.exception.ServiceException;
import com.xoms.util.AppTextUtil;
import com.xoms.util.StringUtil;

@Component
public class PayController {

	Logger logger = LoggerFactory.getLogger(PayController.class);

	@Autowired
	PayService payServiceImpl;

	@Autowired
	PayCacheDao payCacheImpl;

	/**
	 * 微信刷卡支付
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult wxScanAuthCode(GetWXScanAuthCode2StoreParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String orderNo = param.getOrderNo();
			String spbill_create_ip = AppTextUtil.getIpAddr(request);
			String authCode = param.getAuthCode();
			result = payServiceImpl.wxScanAuthCode(orderNo, spbill_create_ip, authCode);
		} catch (ServiceException e) {
			result.setServiceException(e);
			logger.error(e.getMessage(), e);
		} catch (Exception e) {
			result.setFlag(false);
			result.setMsg(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 微信扫码支付
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<String> wxScanQrcode(GetWXScanQrcode2StoreParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<String> result = new ObjectResult<String>();
		try {
			String orderNo = param.getOrderNo();
			String spbill_create_ip = AppTextUtil.getIpAddr(request);
			result = payServiceImpl.wxScanQrcode(orderNo, spbill_create_ip);
		} catch (ServiceException e) {
			result.setServiceException(e);
			logger.error(e.getMessage(), e);
		} catch (Exception e) {
			result.setFlag(false);
			result.setMsg(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 支付宝当面付
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult aliScanAuthCode(GetALIScanAuthCode2StoreParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String orderNo = param.getOrderNo();
			String authCode = param.getAuthCode();
			result = payServiceImpl.aliScanAuthCode(orderNo, authCode);
		} catch (ServiceException e) {
			result.setServiceException(e);
			logger.error(e.getMessage(), e);
		} catch (Exception e) {
			result.setFlag(false);
			result.setMsg(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 支付宝扫码支付
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<String> aliScanQrcode(GetALIScanQrcode2StoreParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<String> result = new ObjectResult<String>();
		try {
			String orderNo = param.getOrderNo();
			result = payServiceImpl.aliScanQrcode(orderNo);
		} catch (ServiceException e) {
			result.setServiceException(e);
			logger.error(e.getMessage(), e);
		} catch (Exception e) {
			result.setFlag(false);
			result.setMsg(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 查询订单支付状态
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<ROrderStatusBean> queryOrderStatus(GetQueryOrderStatusParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<ROrderStatusBean> result = new ObjectResult<ROrderStatusBean>();
		String orderNo = param.getOrderNo();
		try {
			String cacheFlag = payCacheImpl.getPayOrderQueryFlag(orderNo);
			if (!StringUtil.isEmpty(cacheFlag)) {
				result.setFlag(false);
				result.setMsg("正在查询中");
			} else {
				// 设置查询标志
				payCacheImpl.setPayOrderQueryFlag(orderNo);

				result = payServiceImpl.queryOrderStatus(orderNo);

				// 清除缓存标志
				payCacheImpl.clearPayOrderQueryFlag(orderNo);
			}
		} catch (ServiceException e) {
			// 清除缓存标志
			payCacheImpl.clearPayOrderQueryFlag(orderNo);
			result.setServiceException(e);
			logger.error(e.getMessage(), e);
		} catch (Exception e) {
			// 清除缓存标志
			payCacheImpl.clearPayOrderQueryFlag(orderNo);
			result.setFlag(false);
			result.setMsg(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return result;
	}

}
